import marimo

__generated_with = "0.9.27"
app = marimo.App(width="medium")


@app.cell
def __():
    import csv
    import matplotlib.pyplot as plt
    from matplotlib.pyplot import MultipleLocator
    from datetime import datetime
    return MultipleLocator, csv, datetime, plt


@app.cell
def __(mo):
    mo.md(r"""#如何使用pandas将数据一次读出,简化代码?""")
    return


@app.cell
def __(csv):

    # f_name = 'data/sitka_weather_2018_simple.csv'

    f_name = 'C:/Users/xiaobojie/Nutstore/1/study_pro/python_code/待认领数据/data/sitka_weather_07-2018_simple.csv'

    with open(f_name) as f:

        # reader是一个生成器?
        # 生成器是<class 'generator'>
        # reader是一个_csv.reader对象,暂时也不明白是什么
        reader = csv.reader(f)   
        
        header_row = next(reader)   #next 返回下一行的内容，第一次使用，返回第一行
    #     print(next(reader))
    print(header_row)
    print(reader)
    # print(next(reader))

    type(reader)
    # type(header_row)
    return f, f_name, header_row, reader


@app.cell
def __(csv, f_name):
    # f_name_1 = 'data/sitka_weather_07-2018_simple.csv'
    with open(f_name) as f_1:
        reader_1 = csv.reader(f_1)
        header_row_1 = next(reader_1)
    # for index, colu_header in enumerate(header_row_1):
    #     print(index, colu_header)

    header_row_1
    return f_1, header_row_1, reader_1


@app.cell
def __(csv, f_name):
    # f_name_2 = 'data/sitka_weather_07-2018_simple.csv'
    with open(f_name) as f_2:
        reader_2 = csv.reader(f_2)
        header_row_2 = next(reader_2)
        h_temp_l = []
        for row in reader_2:
            h_temp = int(row[5])
            h_temp_l.append(h_temp)
    print(h_temp_l)
    return f_2, h_temp, h_temp_l, header_row_2, reader_2, row


@app.cell
def __(h_temp_l, plt):


    # 绘制数据
    # plt.style.use('seaborn')
    fig, ax = plt.subplots()
    ax.plot(h_temp_l, c='red')

    # 绘制标签标题等
    plt.title('daily hight temperature, july 2018', fontsize=24)
    plt.xlabel('', fontsize=16)
    plt.ylabel('temperature(f)', fontsize=16)
    plt.tick_params(axis = 'both', which = 'major', labelsize=16)
    plt.show()
    return ax, fig


@app.cell
def __(MultipleLocator, csv, datetime, f_name, plt):
    # magic command not supported in marimo; please file an issue to add support
    # %matplotlib
    # import matplotlib.pyplot as plt
    # from matplotlib.pyplot import MultipleLocator
    # from datetime import datetime
    # 
    #读取文档内数据并处理
    # f_name = 'data/sitka_weather_07-2018_simple.csv'
    with open(f_name) as _f:
        _reader = csv.reader(_f)      
        _header_row = next(_reader)
        _date_l, _h_temp_l = [], []
        for _row in _reader:
            _cur_date = datetime.strptime(_row[2], '%Y-%m-%d') # 将日期按格式转换为字符串
            _h_temp = int(_row[5])
            _h_temp_l.append(_h_temp)
            _date_l.append(f'{_cur_date}'[:10])  # 必须这样写，否则日期错误，不知道为什么
    #         print(cur_date)
    #         print(date_l)
    #         break
    # print(h_temp_l)
    # print(date_l)

    # 绘制数据
    # plt.style.use('seaborn')
    _fig, _ax = plt.subplots()
    _ax.plot(_date_l, _h_temp_l, c='red')

    # 绘制标签标题等
    plt.title('daily hight temperature, july 2018', fontsize=24)
    plt.xlabel('', fontsize=16)



    _fig.autofmt_xdate()  # 使x轴图标倾斜显示，不覆盖
    x_major_locator=MultipleLocator(5) #设置x轴标签显示间隔
    _ax.xaxis.set_major_locator(x_major_locator)
    plt.ylabel('temperature(f)', fontsize=16)
    plt.tick_params(axis = 'both', which = 'major', labelsize=16)
    plt.show()
    return (x_major_locator,)


@app.cell
def __(MultipleLocator, csv, datetime, f_name, plt):
    # import matplotlib.pyplot as plt
    # from datetime import datetime

    #读取文档内数据并处理
    # f_name = 'data/sitka_weather_2018_simple.csv'
    # with open(f_name) as f:
    #     reader = csv.reader(f)      
    #     header_row = next(reader)
    #     date_l, h_temp_l = [], []
    #     for row in reader:
    #         cur_date = datetime.strptime(row[2], '%Y-%m-%d') # 将日期按格式转换为字符串
    #         h_temp = int(row[5])
    #         h_temp_l.append(h_temp)
    #         date_l.append(f'{cur_date}'[:10])  # 必须这样写，否则日期错误，不知道为什么
    #         print(cur_date)
    #         print(date_l)
    #         break
    # print(h_temp_l)
    # print(date_l)


    with open(f_name) as _f:
        _reader = csv.reader(_f)      
        _header_row = next(_reader)
        _date_l, _h_temp_l = [], []
        for _row in _reader:
            _cur_date = datetime.strptime(_row[2], '%Y-%m-%d') # 将日期按格式转换为字符串
            _h_temp = int(_row[5])
            _h_temp_l.append(_h_temp)
            _date_l.append(f'{_cur_date}'[:10]) 











    # 绘制数据
    # plt.style.use('seaborn')
    _fig, _ax = plt.subplots()
    _ax.plot(_date_l, _h_temp_l, c='red')

    # 绘制标签标题等
    plt.title('daily hight temperature, 2018', fontsize=24)
    plt.xlabel('', fontsize=16)
    _fig.autofmt_xdate()  
    _x_major_locator=MultipleLocator(30) #设置x轴标签显示间隔
    #此处 x轴标签如何设置整月
    _ax.xaxis.set_major_locator(_x_major_locator)
    plt.ylabel('temperature(f)', fontsize=16)
    plt.tick_params(axis = 'both', which = 'major', labelsize=16)
    plt.show()
    return


@app.cell
def __(MultipleLocator, csv, datetime, f_name, fig, plt):
    #读取文档内数据并处理
    # f_name = 'data/sitka_weather_2018_simple.csv'
    with open(f_name) as _f:
        _reader = csv.reader(_f)      
        _header_row = next(_reader)
        date_l_3, h_temp_l_3, l_temp_l_3 = [], [], []
        for _row in _reader:
            _cur_date = datetime.strptime(_row[2], '%Y-%m-%d') # 将日期按格式转换为字符串
            _h_temp = int(_row[5])
            _l_temp = int(_row[6])
            h_temp_l_3.append(_h_temp)
            date_l_3.append(f'{_cur_date}'[:10])  # 必须这样写，否则日期错误，不知道为什么
            l_temp_l_3.append(_l_temp)
    #         print(cur_date)
    #         print(date_l)
    #         print(l_temp_l)
    #         break
    # print(h_temp_l)
    # print(date_l)

    # 绘制数据
    # plt.style.use('seaborn')
    _fig, _ax = plt.subplots()
    _ax.plot(date_l_3, h_temp_l_3, c='red')
    _ax.plot(date_l_3, l_temp_l_3, c='blue')
    # 绘制标签标题等
    plt.title('daily high and low temperature, 2018', fontsize=24)
    plt.xlabel('', fontsize=16)
    fig.autofmt_xdate()  
    _x_major_locator=MultipleLocator(30) #设置x轴标签显示间隔
    #此处 x轴标签如何设置整月
    _ax.xaxis.set_major_locator(_x_major_locator)
    plt.ylabel('temperature(f)', fontsize=16)
    plt.tick_params(axis = 'both', which = 'major', labelsize=16)
    plt.show()
    return date_l_3, h_temp_l_3, l_temp_l_3


@app.cell
def __(MultipleLocator, date_l_3, h_temp_l_3, l_temp_l_3, plt):


    # 绘制数据
    # plt.style.use('seaborn')
    _fig, _ax = plt.subplots()
    _ax.plot(date_l_3, h_temp_l_3, c='red', alpha=0.5) #alpha为颜色透明度
    _ax.plot(date_l_3, l_temp_l_3, c='blue', alpha=0.5)
    plt.fill_between(date_l_3, h_temp_l_3, l_temp_l_3, facecolor='blue', alpha=0.1)# 增加曲线间填充
    # 绘制标签标题等
    plt.title('daily high and low temperature, 2018', fontsize=24)
    plt.xlabel('', fontsize=16)
    _fig.autofmt_xdate()  
    _x_major_locator=MultipleLocator(30) #设置x轴标签显示间隔
    #此处 x轴标签如何设置整月
    _ax.xaxis.set_major_locator(_x_major_locator)
    plt.ylabel('temperature(f)', fontsize=16)
    plt.tick_params(axis = 'both', which = 'major', labelsize=16)
    plt.show()
    return


@app.cell
def __(MultipleLocator, csv, datetime, plt):

    #读取文档内数据并处理
    # _f_name = 'data/death_valley_2018_simple.csv'

    _f_name = 'C:/Users/xiaobojie/Nutstore/1/study_pro/python_code/待认领数据/data/death_valley_2018_simple.csv'
    with open(_f_name) as _f:
        _reader = csv.reader(_f)    
        _header_row = next(_reader)
        date_l_4, h_temp_l_4, l_temp_l_4 = [], [], []
        for _row in _reader:
            _cur_date = datetime.strptime(_row[2], '%Y-%m-%d') # 将日期按格式转换为字符串
            try:
                _h_temp = int(_row[4])
                _l_temp = int(_row[5])
            except ValueError:
                print(f'missing data for {_cur_date}')
            else:
                h_temp_l_4.append(_h_temp)
                date_l_4.append(f'{_cur_date}'[:10])  # 必须这样写，否则日期错误，不知道为什么
                l_temp_l_4.append(_l_temp)
    #         print(cur_date)
    #         print(date_l)
    #         print(l_temp_l)
    #         break
    # print(h_temp_l)
    # print(date_l)

    # 绘制数据
    # plt.style.use('seaborn')
    _fig, _ax = plt.subplots()
    _ax.plot(date_l_4, h_temp_l_4, c='red', alpha=0.5) #alpha为颜色透明度
    _ax.plot(date_l_4, l_temp_l_4, c='blue', alpha=0.5)
    plt.fill_between(date_l_4, h_temp_l_4, l_temp_l_4, facecolor='blue', alpha=0.1)# 增加曲线间填充
    # 绘制标签标题等
    _title = 'daily high and low temperatures, 2018\ndeath vally, ca'
    plt.title(_title, fontsize=20)
    plt.xlabel('', fontsize=16)
    _fig.autofmt_xdate()  
    _x_major_locator=MultipleLocator(30) #设置x轴标签显示间隔
    #此处 x轴标签如何设置整月
    _ax.xaxis.set_major_locator(_x_major_locator)
    plt.ylabel('temperature(f)', fontsize=16)
    plt.tick_params(axis = 'both', which = 'major', labelsize=16)
    plt.show()
    return date_l_4, h_temp_l_4, l_temp_l_4


@app.cell
def __():
    import marimo as mo
    return (mo,)


if __name__ == "__main__":
    app.run()
